package com.sec.android.app.myfiles.activity;

import android.app.Activity; 
import android.app.ActivityManager;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import android.view.WindowManager;

import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.operation.FileOperator;

/**
 * <pre>
 * Abstract class of Activity.
 * it manage process_id for multi-instance and init facade
 * </pre>
 *
 * @author jae.bae
 */
public abstract class AbsMyFilesActivity extends Activity {
    private static SparseArray<AbsMyFilesActivity> sActivityMap = new SparseArray<>();

    /**
     * static variable for create ProcessID
     */
    private static int sNewProcessId = 0;

    private int mDensityDpi = Configuration.DENSITY_DPI_UNDEFINED;

    /**
     * variable of current Activity, it should get by getProcessId()
     */
    private int mProcessId = -1;

    /**
     * static variable of current focused Activity, it should get by getFocusedProcessId()
     */
    private static int sFocusedProcessId = -1;

    /**
     * create and return ProcessId of current Activity. This value use a key of singleton object
     *
     * @see NavigationManager
     */
    public final int getProcessId() {
        if (mProcessId < 0) {
            mProcessId = getNewProcessId();
        }
        return mProcessId;
    }

    public static int getFocusedProcessId() {
        return sFocusedProcessId;
    }

    private int getNewProcessId() {
        return sNewProcessId++;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        MyFilesFacade.create(getProcessId());
        initDensityDpi();
        if (savedInstanceState != null) {
            FileOperator.init();
        }
        sFocusedProcessId = getProcessId();
        super.onCreate(null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        sActivityMap.put(getProcessId(), this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        int processId = getProcessId();
        MyFilesFacade.destroy(processId);
        FileOperator.destroy(processId);
        sActivityMap.remove(processId);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if (hasFocus) {
            sFocusedProcessId = getProcessId();
        }
    }

    public static AbsMyFilesActivity getMyFilesActivity(int processId) {
        return sActivityMap.get(processId);
    }

    public boolean isPinWindow() {
        boolean bRet = false;
        ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(Activity.ACTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (am != null && am.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_PINNED) {
                bRet = true;
            }
        }
        return bRet;
    }

    public static int getMaxProcessId() {
        return sNewProcessId;
    }

    private void initDensityDpi() {
        DisplayMetrics metrics = new DisplayMetrics();
        ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
        mDensityDpi = metrics.densityDpi;
    }

    public int getDensityDpi() {
        return mDensityDpi;
    }

    public void setDensityDpi(int densityDpi) {
        mDensityDpi = densityDpi;
    }

}
